// pages/address/address.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    defaultid:0,
    refresh:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.refresh){
      this.Find_Address();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  Find_Address: function () {
    var that = this;
    var data = {};
    data.act = "Find_Address";
    wx.showLoading({
      title: '加载中...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      var defaultid=0;
      for(var i=0;i<json.data.length;i++){
        if(json.data[i].IsDefault==1){
          defaultid = json.data[i].ID;
        }
      }
      that.setData({ list: json.data, defaultid:defaultid });
    });
  },
  Set_IsDefault:function(e){
    var defaultid = e.currentTarget.dataset.defaultid;
    var that = this;
    var data = {};
    data.id = defaultid;
    data.act = "Set_IsDefault";
    wx.showLoading({
      title: '正在设置...',
    })
    app.Ajax(data, function (json) {
      wx.hideLoading();
      if(json.state){
        that.setData({defaultid: defaultid });
        app.showSuccess(json.msg);
      }else{
        app.showError(json.msg);
      }
    });
  },
  Delete_Address :function(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    wx.showModal({
      title: '提示',
      confirmColor: '#fb4747',
      content: '您确定要删除吗？',
      success: function (res) {
        if (res.confirm) {
          var data = {};
          data.ids = id;
          data.act = "Delete_Address";
          wx.showLoading({
            title: '删除中...',
          })
          app.Ajax(data, function (json) {
            wx.hideLoading();
            if (json.state) {
              var list=that.data.list;
              for (var i = list.length-1;i>=0;i--){
                if(list[i].ID==id){
                  list=list.RemoveAt(i);
                }
              }
              that.setData({ list: list});
              app.showSuccess(json.msg);
            } else {
              app.showError(json.msg);
            }
          });
        }
      }
    })
  },
  selectAddress: function () {
    var that = this;
    this.setData({ refresh: false });
    wx.chooseAddress({
      success: function (res) {
        var data = {};
        data.province = res.provinceName;
        data.city = res.cityName;
        data.area = res.countyName;
        data.realname = res.userName;
        data.mobile = res.telNumber;
        data.address = res.detailInfo;
        data.act = "Save_Address";
        wx.showLoading({
          title: '正在保存...',
        })
        app.Ajax(data, function (json) {
          wx.hideLoading();
          if (json.state) {
            that.Find_Address();
          } else {
            app.showError(json.msg);
          }
        });
      }
    })
  },
  Edit_Address:function(e){
    var id = e.currentTarget.dataset.id;
    this.setData({refresh:false});
    wx.navigateTo({
      url: '/pages/addaddress/addaddress?id=' + id
    })
  }
})